<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
var status = "<c:out value='${status}'/>";
$(function(){
	//弹出框设置
	$("#dialogDiv").dialog({
		title:"公文信息",//标题
		width: 800,//宽度
	    height: 400,//高度
	    closed: true,//初始关闭
	    cache: false,//缓存
	    modal: true,
	    buttons:[
			{
				text:'关闭',
				handler:function(){$('#dialogDiv').dialog("close");}
			}
		],
	    onClose: function() {
			 $("#dialogIframe").attr("src","");
			} 
	});	
	
	//查询按钮绑定点击事件
	$("#searchButton").click(function(){
		$('#synFromTable').datagrid({ queryParams:getParams()});
	});
	
	//删除按钮绑定点击事件
	$("#deleteButton").click(function(){
		if($("input[name='docId']:checked").size()==1){
			$.messager.confirm('提示', '确定要删除数据吗?', function(r){
				if (r){
					$.ajax({
						   type: "POST",
						   url: "<c:url value='/receive/updateIssueState' />",
						   data: "docId="+$("input[name='docId']:checked").val(),
						   success: function(msg){
							   if(msg.state=="200"){
								   $.messager.alert('提示','数据删除成功');
								   $('#synFromTable').datagrid({ queryParams:getParams()});
							   }else{
								   if(msg.state=="600"){
									   $.messager.alert('提示',"数据删除失败，该数据状态已改变");
									   $('#synFromTable').datagrid({ queryParams:getParams()});
								   }else{
									   $.messager.alert('提示',"数据删除失败");
								   }
								   
							   }
						   }
						});
				}
			});
		}else{
			$.messager.alert('提示','请选择一条数据再删除');
		}
	});
	
	//待办分发按钮绑定点击事件
	$("#receviceButton").click(function(){
		if("<c:out value='${isSign}'/>"=="1"){
			$.messager.defaults = { ok: "是", cancel: "否" };
			$.messager.confirm({
				title:'提示', 
	    		msg:'进入之后只能由本人进行拟办分发，是否进行分发？',
	    		modal: true,
	    		//top:50,
	    		fn:function(r){
	    			if(r){
	    				if($("input[name='docId']:checked").size()==1){
	    					var $checked = $(':radio:checked');
	    					var $tr = $checked.closest('tr');
	    					var formId=$tr.find('td:eq(9)').text();
	    					if(formId==""){
	    						$.messager.alert('提示','请检查发文抬头是否配置正确');
	    						return;
	    					}
	    					//alert(formId);
	    					var issueId=$("input[name='docId']:checked").val();
	    					if(issueId==""){
	    						$.messager.alert('提示','获取数据错误');
	    						return;
	    					}
//	    					window.location.href=_synergyContext+"/basic/flowItem/"+formId+"/isNew?issueId="+issueId; 
	    					
	    					$.ajax({//ajax保存表单基本信息
	    						   type: "POST",
	    						   url: "<c:url value='/receive/receviceWorkItemsTodo' />",
	    						   data: "issueId="+issueId,
	    						   dataType: "json",
	    						   async:false,
	    						   success: function(msg){
	    						     if(msg.state=="200"){
	    						    	 window.location.href=_synergyContext+msg.url;
	    						     }else{
	    						    	 $.messager.alert("提示","分发失败");
	    						     }
	    						   }
	    						});
	    				}else{
	    					$.messager.alert('提示','请选择一条数据再分发');
	    				}
	    			}
	    		}
			});
		}else{
			$.messager.alert('提示',"您没有拟办分发权限");
		}
	});
	
	//拒收按钮绑定点击事件
	$("#refusedButton").click(function(){
		if($("input[name='docId']:checked").size()==1){
			var issueId=$("input[name='docId']:checked").val();
			var $checked = $(':radio:checked');
			var $tr = $checked.closest('tr');
			if($tr.find('td:eq(10)').text() == "0001" ){
				//属于国家局来文，不能进行拒收操作
				$.messager.alert("提示","您不能拒收国家局已收文件!");
				return ;
			}

			$("#newcome").dialog("open");
			
			/*var title = $tr.find('td').eq(2).text();
			var receiveInfo = "已拒收《"+title+"》";
			$("#receiveInfo").textbox("setValue", receiveInfo);
			$('#newcome').dialog({
				width:500,
				height:200,
				title:"拒收来文",
				resizable:false,
				buttons:[{
						text:'确定',
						handler:function(){
							if(confirm("您确定需要拒收已签收的文件吗?")){
								$.ajax({
									type:"POST",
									url:'<c:url value="/receive/signInAfterRefused"/>',
									data: "docId="+$("input[name='docId']:checked").val()+"&receiveInfo="+$("#receiveInfo").textbox('getValue'),
									success:function(msg){
										if(msg.state=="200"){
											   $('#newcome').dialog( "close" );
											   $('#synFromTable').datagrid({ queryParams:getParams()});
										   }else{
											   $.messager.alert('提示',"拒收失败");
										   }										
									}
								});
							}
						}
					},{
						text:'关闭',
						handler:function(){$('#newcome').dialog("close");}
					}
					]
			});*/
		}else{
			$.messager.alert('提示','请选择一条数据再拒收');
		}
	});
		
	//表格加载
	$('#synFromTable').datagrid({
	    url:'<c:url value="/receive/getReceviceWorkItems"/>',
	    queryParams:getParams(),//提交参数
	    toolbar:'#synFromToolbar',//表头工具栏
	    pagination:true,//显示分页  
	    pagePosition:'bottom',//分页栏出现位置
	    pageNumber:1,//初始页码
	    pageSize:10,//初始每页显示记录数量
	    singleSelect:true,
	    nowrap: false,//设置内容多时换行，false换行
	    columns:[[
			{field:'docId',title:'选择',formatter: function(value,row,index){
				return '<input name="docId" type="radio" value="'+value+'" />';
			},width:"4%"},
			{field:'index',title:'序号',formatter: function(value,row,index){
				return (index+1);
			},width:"4%"},
			{field:'displayNumberReceive',title:'编号',width:"18%"},
			{field:'docName',title:'公文标题',width:"28%",formatter: function(value,row,index){
				var emergency = row.emergencyCode;
				if(emergency){
                	if(emergency=="普通"){
                		emergency = "["+emergency+"]";
                	}else{
                		emergency = "<span style='color:red'>["+emergency+"]</span>";
                    }
                }else{
                	emergency = "";
                }
				if(status=="TS"){
					return "<a href='javascript:void(0)' onclick=toEdit('"+row.formId+"','"+row.docId+"') >"+value+"</a>"+emergency;
				}
				return "<a href='javascript:void(0)' onclick=toDistribute('"+row.formId+"','"+row.docId+"') >"+value+"</a>"+emergency;
			}},
			{field:'dispatchOrg',title:'来文单位',width:"12%"},
			{field:'docReceiveCode',title:'收文种类',width:"18%",formatter: function(value,row,index){
				if(value=='办理类'||value=='阅知类'){
					return "行政收文";
				}else{
					return value;
				}
			}},
			{field:'displayNumber',title:'来文编号',width:"10%"},
			{field:'createTime',title:'来文时间',width:"9%",formatter: function(value,row,index){
				var tempDate="";				
				if(value){
					tempDate=dataFormatStr(new Date(value),"yyyy-MM-dd hh:mm:ss");
				}
				return tempDate;
			}},
			
			{field:'fromTime',title:'收文时间',width:"9%",formatter: function(value,row,index){
				var tempDate="";				
				if(value){
					tempDate=dataFormatStr(new Date(value),"yyyy-MM-dd hh:mm:ss");
				}
				return tempDate;
			}},
			{field: "formId" ,hidden:"true"},
			{field: "dispatchOrgCode" ,hidden:"true"},
			{field:'docInfo',title:'公文信息',formatter: function(value,row,index){
				return "<a href='javascript:void(0)' onclick='openDocInfo(this)'>查看</a>";
			}}
	    ]],
	    onClickRow:function(index,row){
	    	$("input[type=radio]").val([row.docId]).attr("checked",true);
	    }
	});
	
	$("#newcome").dialog({
		title:"公文拒收",//标题
		width: 500,//宽度
	    height: 290,//高度
	    closed: true,//初始关闭
	    cache: false,//缓存
	    modal: true,
	    onClose: function() {
			 $("#receiveInfo").textbox("setValue","");
			} ,
	 buttons:[{
		 text:'确认',
			handler:function(){
				if($("#reasonForm").form('validate')){
					$.ajax({
						   type: "POST",
						   url: '<c:url value="/receive/signInAfterRefused"/>',
						   data: "docId="+$("input[name='docId']:checked").val()+"&receiveInfo="+$("#receiveInfo").textbox('getValue'),
						   success: function(msg){
							   if(msg.state=="200"){
								    $.messager.alert('提示',"拒收成功");
								   $("#newcome").dialog("close");
								   $('#synFromTable').datagrid({ queryParams:getParams()});
							   }else{
								   $.messager.alert('提示',"拒收失败");
							   }
						   }
						});
				}
			}
	 },{
		 text:'取消',
			handler:function(){
				$("#newcome").dialog("close");
			}
	 }]
	    });
});

function toDistribute(formId,issueId){
	if("<c:out value='${state}'/>"=="ND"){
		if("<c:out value='${isSign}'/>"=="1"){
			$.messager.defaults = { ok: "是", cancel: "否" };
			$.messager.confirm({
				title:'提示', 
				modal: true,
				msg:'进入之后只能由本人进行拟办分发，是否进行分发？', 
				//top:50,
				fn:function(r){
					if(r){
						$.ajax({//ajax保存表单基本信息
							   type: "POST",
							   url: "<c:url value='/receive/receviceWorkItemsTodo' />",
							   data: "issueId="+issueId,
							   dataType: "json",
							   async:false,
							   success: function(msg){
							     if(msg.state=="200"){
							    	 window.location.href=_synergyContext+msg.url;
							     }else if(msg.state=="201"){
							    	 $.messager.alert("提示","该数据已分发", "info", function () {
							    		 window.location.reload();
							         });
							     }else{
							    	 $.messager.alert("提示","分发失败");
							     }
							   }
							});
					}
				}
			});
		}else{
			$.messager.alert('提示',"您没有拟办分发权限");
		}
	}else{
		var url=_synergyContext+"/basic/donePage?issueId="+issueId+"&formId="+formId+"&backType=close"; 
		var winFeatures = "height="+(window.screen.height-80)+"px,width="+(window.screen.width-20)+"px,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no";
		window.open(url,'_blank',winFeatures);
	}		
	//window.location.href=_synergyContext+"/basic/flowItem/"+formId+"/isNew?issueId="+issueId; 
}

function toEdit(formId,issueId){
	window.location.href="<c:url value='/receive/editPaperIssue' />"+"?issueId="+issueId; 
}

//关闭弹出框
function closeDialog(){
	$('#synFromTable').datagrid({ queryParams:getParams()});
	$('#dialogDiv').dialog('close');
}

//查看公文信息
function openDocInfo(obj){
	var docId = $(obj).closest('tr').find(':radio').val();
	$("#dialogIframe").attr("src","<c:url value='/receive/viewDocIssueInfo'/>?docId="+docId);
	$('#dialogDiv').dialog('open').window('center');
	
	/*var vHeight=320;                          //弹出窗口的宽度; 
    var vWidth=800;                         //弹出窗口的高度; 
    //获得窗口的垂直位置 
    var iTop = (window.screen.availHeight - 30 - vHeight) / 2; 
    //获得窗口的水平位置 
    var iLeft = (window.screen.availWidth - 10 - vWidth) / 2; 
	var url="<c:url value='/receive/viewDocIssueInfo'/>?docId="+docId;
	var winFeatures = "height="+vHeight+"px,width="+vWidth+"px,top=" + iTop + ",left=" + iLeft + ",toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no";
	window.open(url,'_blank',winFeatures);	*/	
}

//查询条件生成方法
function getParams(){
	var jsonObj=new Object();
	if($('input[name="docName"]').val()!=""){
		jsonObj.docName=$('input[name="docName"]').val();
	}
	if($('input[name="displayNumber"]').val()!=""){
		jsonObj.displayNumber=$('input[name="displayNumber"]').val();
	}
	if($('input[name="dispatchOrg"]').val()!=""){
		jsonObj.dispatchOrg=$('input[name="dispatchOrg"]').val();
	}
	if($('input[name="createTime"]').val()!=""){
		jsonObj.createTime=$('input[name="createTime"]').val();
	}
	if($('input[name="displayNumberReceive"]').val()!=""){
		jsonObj.displayNumberReceive=$('input[name="displayNumberReceive"]').val();
	}
	if($('input[name="fromTime"]').val()!=""){
		jsonObj.fromTime=$('input[name="fromTime"]').val();
	}
	if('<c:out value="${type}"/>'!=""){
		jsonObj.docReceiveType='<c:out value="${type}"/>';
	}
	
	jsonObj.docState='<c:out value="${state}"/>';
	jsonObj.dispatchOrgCode='<c:out value="${dispatchOrgCode}"/>';
	jsonObj.dispatchOrgCodeNE='<c:out value="${dispatchOrgCodeNE}"/>';
	return jsonObj;
}
